part of my_base_lib;

class DioUtils {
  static final Dio dio = Dio();


  static cancel(CancelToken cancelToken){
    try{
      cancelToken.cancel();
    }catch(e){
      //
    }
  }

  /// get请求
  ///
  /// headerParameters 请求头参数
  /// parameters 请求体参数
  /// cancelToken 用于取消请求的字段
  static Future<Response> baseGet(String url,
      {Map<String, dynamic>? headerParameters,
      Map<String, dynamic>? parameters,
      CancelToken? cancelToken}) async {
    //头信息（修改Cookie或添加其他头字段等操作）
    if (headerParameters != null) {
      dio.options.headers.addAll(headerParameters);
    }
    return await dio.get(url,
        queryParameters: parameters, cancelToken: cancelToken);
  }

  /// 表单格式 post请求
  ///
  /// headerParameters 请求头参数
  /// parameters 请求体参数
  /// cancelToken 用于取消请求的字段
  static Future<Response> baseFormPost(String url,
      {Map<String, dynamic>? headerParameters,
        Map<String, dynamic>? parameters,
        CancelToken? cancelToken}) async {
    //头信息（修改Cookie或添加其他头字段等操作）
    if (headerParameters != null) {
      dio.options.headers.addAll(headerParameters);
    }
    FormData formData = FormData.fromMap(parameters??{});
    return await dio.post(url, data: formData, cancelToken: cancelToken);
  }



  /// json格式 post请求
  ///
  /// headerParameters 请求头参数
  /// parameters 请求体参数
  /// cancelToken 用于取消请求的字段
  static Future<Response> baseJsonPost(String url,
      {Map<String, dynamic>? headerParameters,
      Map<String, dynamic>? parameters,
      CancelToken? cancelToken}) async {
    //头信息（修改Cookie或添加其他头字段等操作）
    if (headerParameters != null) {
      dio.options.headers.addAll(headerParameters);
    }
    return await dio.post(url, data: parameters, cancelToken: cancelToken);
  }


  ///下载
  static download(String url,String savePath,{Function? onSuccess,Function(int progress)? onProgress,Function(String msg)? onFailed})async{
    try {
      await dio.download(url, savePath, onReceiveProgress: (count, total) {
        if (total != -1) {
          int progress =(count / total * 100).toInt();
          onProgress?.call(progress);
        }
      });
      onSuccess?.call();
    } catch (e) {
      onFailed?.call(e.toString());
    }
  }



  ///上传
  static upload(String url,List<File> files,Map<String, dynamic>? parameters,{Function? onSuccess,Function(int progress)? onProgress,Function(String msg)? onFailed}) async{
    try{
      parameters??={};
      if(files.length==1){
        parameters['file'] = MultipartFile.fromFile(files[0].path, filename: path.basename(files[0].path));
      }
      if(files.length>1){
        parameters['files'] = [];
        for (var item in files) {
          (parameters['files'] as List).add(MultipartFile.fromFile(item.path, filename: path.basename(item.path)));
        }
      }
      final formData = FormData.fromMap(parameters);

      await dio.post(url, data: formData,onSendProgress: (count, total) {
        if (total != -1) {
          int progress =(count / total * 100).toInt();
          onProgress?.call(progress);
        }
      }, );
      onSuccess?.call();
    }catch(e){
      onFailed?.call(e.toString());
    }
  }

}

